package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.mapper.MyordersMapper;
import com.chixing.pojo.Myorders;
import com.chixing.service.IMyordersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户购买简历记录（订单） 服务实现类
 * </p>
 *
 * @author Jason Marslyon
 * @since 2022-10-04
 */
@Service
public class MyordersServiceImpl  implements IMyordersService {

    @Autowired
    private MyordersMapper myordersMapper;

    @Override
    public Boolean insertMyOrder(Myorders myorders) {
        int insert = myordersMapper.insert(myorders);
        return insert>0;
    }

    @Override
    public Myorders selectById(Integer id) {
        return myordersMapper.selectById(id);
    }

    @Override
    public Myorders selectByOrderNumber(String orderId) {
        QueryWrapper<Myorders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_number",orderId);
        Myorders myorders = myordersMapper.selectOne(queryWrapper);
        return myorders;
    }

    @Override
    public List<Myorders> selectAllById(Integer userId) {
        QueryWrapper<Myorders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("person_id",userId);
        List<Myorders> myOrdersList = myordersMapper.selectList(queryWrapper);
        return myOrdersList;
    }

    @Override
    public Myorders selectByOrderId(Integer id) {
        return myordersMapper.selectById(id);
    }

    @Override
    public boolean updateMyOrder(Myorders myorders) {
        return myordersMapper.updateById(myorders)>0;
    }

    @Override
    public boolean deleteByOrderNumber(String orderNumber) {
        QueryWrapper<Myorders> wrapper = new QueryWrapper<>();
        wrapper.eq("order_number",orderNumber);
        Myorders myorders = myordersMapper.selectOne(wrapper);

        //如果订单已经支付，返回false，否则删除
        if(myorders.getOrderState() == 1){
            //已经支付，已产生订单流水，删除不了该订单记录
            return false;
        }else {
            return myordersMapper.deleteById(myorders)>0;
        }

    }
}

